package com.xgj.ph.servlet;

import com.alibaba.fastjson.JSON;
import com.xgj.ph.dao.CommunityDao;
import com.xgj.ph.dao.ShopDao;
import com.xgj.ph.dao.impl.CommunityDaoImpl;
import com.xgj.ph.dao.impl.ShopDaoImpl;
import com.xgj.ph.entity.Community;
import com.xgj.ph.entity.Shop;
import com.xgj.ph.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.xgj.ph.util.JDBCDruidUtil.update;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    CommunityDao communityDao = new CommunityDaoImpl();
    ShopDao shopDao = new ShopDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  1.设置编码方式
        //  2.读取action，并判断
        String action = req.getParameter("action");
        //  3.根据action执行不同的代码
        if ("selectAll".equals(action)) {
            //  3.1 接收数据
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String communityName = req.getParameter("communityName");
            String communityId = req.getParameter("communityId");
            //  3.2 封装到map集合
            Map<String, String> map = new HashMap<String, String>();
            map.put("communityId",communityId);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("communityName", communityName);
            //  3.3 连接到数据库操作
            List<Community> list = communityDao.selectAll(map);
            int total = communityDao.selectCount(map);
            //  3.4 给出响应信息
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result = Result.success(resultMap);

            resp.getWriter().write(JSON.toJSONString(result));
        }else if("update".equals(action)){
//            System.out.println("进来增改业务了");
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode = 0;
            if(code!=null && !"".equals(code)){
                communityAreaCode = Integer.parseInt(code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String id = req.getParameter("communityId");
            int communityId = 0;
            if(id!=null && !"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
            //  组成对象
            Community community = new Community(communityId, communityName,communityProvince,communityCity,communityDistrict,communityAddress,communityLatitude,communityLongitude,communityAreaCode,communityCoordinates);
            //  判断是新增还是修改
            if ("1".equals(addOrUpdateFlag)) {
                int num = communityDao.insert(community);
                Result result = null;
                if(num!=0){ //   新增成功
                    result = Result.success("新增成功");
                }else{
                    result = Result.error("新增失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }else{
                int num = communityDao.update(community);
                Result result = null;
                if(num!=0){ //   修改成功
                    result = Result.success("修改成功");
                }else{
                    result = Result.error("修改失败");
                }
                resp.getWriter().write(JSON.toJSONString(result));
            }
        }else if("delete".equals(action)){
            String id = req.getParameter("communityId");
            int communityId = 0;
            if(id!=null && !"".equals(id)){
                communityId = Integer.parseInt(id);
            }
            int num = communityDao.delete(communityId);
            Result result = null;
            if(num!=0){ //   新增成功
                result = Result.success("删除成功");
            }else{
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));

        }else if("selectShop".equals(action)){      //  查看关联门店
            //  接收选中小区的数据
            String communityId = req.getParameter("communityId");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopName = req.getParameter("shopName");
            //  封装到Map集合
            Map<String, String> map = new HashMap<String, String>();
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("shopName", shopName);
            map.put("communityId", communityId);
            //  3.3 连接到数据库操作
            List<Shop> list = shopDao.selectAll(map);
            int total = shopDao.selectCount(map);
            //  3.4 给出响应信息
            Map resultMap = new HashMap();
            resultMap.put("list", list);
            resultMap.put("total", total);
            Result result = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("addShop".equals(action)){ //  添加关联门店
            //  关联门店
            String panghuCommunityId = req.getParameter("panghuCommunityId");
            System.out.println("panghuCommunityId="+panghuCommunityId);
            String sid = req.getParameter("fendeShopCommunityList");

            String[] sz = sid.split(",");
            int num = 0;
            for(String shopId : sz){
            System.out.println(shopId);
                num += communityDao.updateShop(Integer.parseInt(panghuCommunityId), Integer.parseInt(shopId));
                System.out.println(num);
        }
//            int update = communityDao.updateShopCount(Integer.parseInt(panghuCommunityId),num);
            int i = shopDao.updateShopCount(Integer.parseInt(panghuCommunityId));
            Result result = null;
            if(num!=0){ //   添加成功
                result = Result.success("添加成功");
            }else{
                result = Result.error("添加失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }else if("deleteShop".equals(action)){  //删除关联门店
            String id =req.getParameter("shopId");
            String cid =req.getParameter("communityId");
            int shopId = 0;
            if (id!=null&&!"".equals(id)){
                shopId = Integer.parseInt(id);
            }
            int communityId = 0;
            if (cid!=null&&!"".equals(cid)){
                communityId = Integer.parseInt(cid);
            }
            int num = shopDao.updateShop(shopId,communityId);
            Result result = null;
            if(num!=0){
                result = Result.success("删除成功");
            }else{
                result = Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));
        }




    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
